<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class AdminUser extends Authenticatable
{
    protected $fillable=['username','password'];
    protected $rememberTokenName = '';//重写字段去掉remembertoken
    //用户有哪些关系
    public function roles()
    {
        return $this->belongsToMany('App\Models\AdminRole','admin_role_user','user_id','role_id')->withPivot([
            'user_id','role_id'
        ]);
    }
    //判断是否有某个角色或某些角色
    public function isInroles($roles)
    {
        return !!$roles->intersect($this->roles)->count();
    }
    //给用户分配角色
    public function assignRole($role)
    {
        return $this->roles()->save($role);
    }
    //取消用户分配的角色
    public function deleteRole($role)
    {
        return $this->roles()->detach($role);
    }
    //判断用户是否有权限
    public function hasPermission($permission)
    {
        return $this->isInroles($permission->roles);
    }



}
